package com.chen.pfm.utils;

import com.chen.pfm.entity.Account;
import com.chen.pfm.entity.ChartData;
import com.chen.pfm.entity.Classify;
import com.chen.pfm.entity.DisplayBill;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.agp.components.Component;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

public class AccountDBUtil extends AbilitySlice {
    static final HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x2022001, "AccountDBUtil");
    static final String uriString = "dataability:///com.chen.pfm.AccountDataAbility/account";





    public static boolean isExistsAccountsByAid(Context context, String Aid) {
        boolean flag = false;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("aid", Aid);
        String[] columns = new String[]{"aid"};
        Uri uri = Uri.parse(uriString);
        //数据库帮助类调用查询，得到结果集
        ResultSet resultSet = null;
        try {
            resultSet = helper.query(uri, columns, predicates);
            if (resultSet.getRowCount() > 0) {
                flag = true;
            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return flag;

    }
    //分类删除的时候需要判断分类是否存在账单
    public static boolean queryIsExistsAccountByCid(Context context, String cid) throws DataAbilityRemoteException {
        boolean flag = false;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        String[] columns = new String[]{"aid"};
        predicates.equalTo("cid", cid);
        Uri uri = Uri.parse(uriString);
        ResultSet resultSet = helper.query(uri, columns, predicates);
        if (resultSet.getRowCount() > 0) {
            // 处理结果
            flag = true;
        }
        return flag;
    }



    public static double statisticalMoneyPastSevenDaysBytype1(Context context, int type1) {
        double amoney = 0.0;
        String[] pastSevenDate = DateUtils.getPastSevenDate();
        for (int i = 0; i < pastSevenDate.length; i++) {
            try {
                amoney += statisticsMoneyByArtimeAndtype1(context, pastSevenDate[i], type1);
            } catch (DataAbilityRemoteException e) {
                e.printStackTrace();
            }

        }
        return amoney;

    }

    public static int statisticalTodayNumberAccounts(Context context) {
        int rowCount = 0;
        String currentYearandMonthandDay = DateUtils.getStrCurrentYearandMonthandDay();
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.beginsWith("actime", currentYearandMonthandDay);
        String[] columns = new String[]{"aid"};
        Uri uri = Uri.parse(uriString);
        //数据库帮助类调用查询，得到结果集
        ResultSet resultSet = null;
        try {
            resultSet = helper.query(uri, columns, predicates);

            rowCount = resultSet.getRowCount();
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return rowCount;

    }

    public static int statisticalMomthNumberAccounts(Context context) {
        int rowCount = 0;
        String currentYearandMonthandDay = DateUtils.getStrCurrentYearandMonth();
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.beginsWith("actime", currentYearandMonthandDay);
        String[] columns = new String[]{"aid"};
        Uri uri = Uri.parse(uriString);
        //数据库帮助类调用查询，得到结果集
        ResultSet resultSet = null;
        try {
            resultSet = helper.query(uri, columns, predicates);

            rowCount = resultSet.getRowCount();
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return rowCount;

    }

    public static int statisticalTotalNumberAccounts(Context context) {
        int rowCount = 0;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        String[] columns = new String[]{"aid"};
        Uri uri = Uri.parse(uriString);
        //数据库帮助类调用查询，得到结果集
        ResultSet resultSet = null;
        try {
            resultSet = helper.query(uri, columns, predicates);

            rowCount = resultSet.getRowCount();
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return rowCount;

    }

    public static double statisticsMoneyByArtimeAndtype1(Context context, String Artime, int type1) throws DataAbilityRemoteException {
        double amoney = 0.0;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.beginsWith("artime", Artime);
        String[] columns = new String[]{"cid", "amoney"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet resultSet = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (resultSet.getRowCount() > 0) {
                // 处理结果
                resultSet.goToFirstRow();
                do {
                    String cid = resultSet.getString(0);
                    Classify classify = ClassifyDBUtil.queryOneClassifyByCid(context, cid);
                    if (classify.getCtype_1() == type1) {
                        amoney += resultSet.getDouble(1);
                    }
                    // 在此处理ResultSet中的记录;
                } while (resultSet.goToNextRow());

            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return amoney;
    }

    public static double statisticsMoneyBy_Cid_Artime_type1(Context context, String findcid, String Artime, int type1) {
        double amoney = 0.0;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造查询条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.beginsWith("artime", Artime);
        predicates.equalTo("cid", findcid);
        String[] columns = new String[]{"cid", "amoney"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet resultSet = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            if (resultSet.getRowCount() > 0) {
                // 处理结果
                resultSet.goToFirstRow();
                do {
                    String cid = resultSet.getString(0);
                    Classify classify = ClassifyDBUtil.queryOneClassifyByCid(context, cid);
                    if (classify.getCtype_1() == type1) {
                        amoney += resultSet.getDouble(1);
                    }
                    // 在此处理ResultSet中的记录;
                } while (resultSet.goToNextRow());

            }
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return amoney;
    }

    public static int statisticsNumberBy_Cid_Artime(Context context, String findcid, String Artime) {
        int n = 0;
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.beginsWith("artime", Artime);
        predicates.equalTo("cid", findcid);
        String[] columns = new String[]{"aid"};
        Uri uri = Uri.parse(uriString);
        try {
            //数据库帮助类调用查询，得到结果集
            ResultSet resultSet = helper.query(uri, columns, predicates);
            //如果结果集合不为空
            n = resultSet.getRowCount();
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return n;
    }

    public static int updateAccount(Context context, Account account) throws DataAbilityRemoteException {
        // 构造插入数据
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        // 构造更新条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("aid", account.getAid());
        Uri uri = Uri.parse(uriString);
        int update = helper.update(uri, account.getValuesBucketfromObject(), predicates);
        return update;
    }

    public static int insertAccount(Context context, Account account) {
        int i = -1;
        if (account.getAid() == null) {
            account.setAid(MyUtil.randomUUID());
        }
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        ValuesBucket valuesBucket = account.getValuesBucketfromObject();
        try {
            i = helper.insert(uri, valuesBucket);
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        return i;
    }



    public static int deleteAccountByAid(Context context, String aid) throws DataAbilityRemoteException {
        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        // 构造删除条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("aid", aid);
        int i = helper.delete(uri, predicates);
        return i;
    }
    public static int deleteByUid(Context context, Integer uid) throws DataAbilityRemoteException {

        DataAbilityHelper helper = DataAbilityHelper.creator(context);
        Uri uri = Uri.parse(uriString);
        // 构造删除条件
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        predicates.equalTo("uid", uid);
        int i = helper.delete(uri, predicates);
        return i;
    }

}
